package com.itboye.mengwu.app.ui.mymeng;

import java.util.Observable;
import java.util.Observer;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.AppConfigModel;
import com.itboye.mengwu.data.model.VIPPersonalBean;
import com.itboye.mengwu.data.model.WithdrawalsBean;
import com.itboye.mengwu.data.presenter.ConfigAppPresenter;
import com.itboye.mengwu.data.presenter.LiShiTiXianPresenter;
import com.itboye.shopcar_adapter.HistoryAdapter;

public class ApplyTiXianActivity extends BaseActivity implements Observer{

	private TextView txtbalance;
	
	private EditText editextNum;
	private Button btn_TiXian;
	
	private LiShiTiXianPresenter presenter;
	
	
	private String uid;
	private String money;
	
	private ImageView back;
	
	private 	ConfigAppPresenter appPresenter;
	private 	VIPPersonalBean vipPersonalBean;
	
	private String num;
	
	private TextView txtBack;
	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_apply_tixian;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		txtBack.setText("原路退还至绑定支付宝");
		appPresenter=new ConfigAppPresenter(this);
		vipPersonalBean=new VIPPersonalBean();
		showProgressDialog("数据加载中,请稍后", true);
		uid= SPUtils.get(ApplyTiXianActivity.this, null,
				SpPublicParams.UID, "") + "";
		appPresenter.getGuanZhu(uid,null);
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		btn_TiXian.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onTiXina();
			}
		});
	}

	public  void onTiXina(){
		presenter=new LiShiTiXianPresenter(this);
		money=editextNum.getText().toString().trim();
	
		if(money==null ){
			ByAlert.alert("请填写提现金额");
		}else if(money==""){
			ByAlert.alert("请填写提现金额");
			try {
				if( Double.parseDouble(money)<=0){
					ByAlert.alert("您输入的金额小于0元，请重新输入提现金额...");
				}
				
			} catch (Exception e) {
				// TODO: handle exception
			}
			
		}else{
			showProgressDialog("正在申请中,请稍后", true);
			presenter.getTiXian(uid, money);
		}
			
	}
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == LiShiTiXianPresenter.ShenQingTiXIan_success) {
				ByAlert.alert(""+entity.getData());
			}else if(entity.getEventType() == LiShiTiXianPresenter.ShenQingTiXIan_fail){
				ByAlert.alert(""+entity.getData());
			}
			else if (entity.getEventType() == ConfigAppPresenter.Guanzhu_success) {
				vipPersonalBean = (VIPPersonalBean) entity.getData();
				num = vipPersonalBean.getBalance();
				txtbalance.setText("￥"+num);
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}
